from django.db import models
from human.models import UserModel
from department.models import Department
from ckeditor_uploader.fields import RichTextUploadingField
import datetime
# Create your models here.



class CouponModel(models.Model):
    DISCOUNT_TYPE_CHOICES = (
        (0, '满减'),
        (1, '折扣'),
    )
    GET_TYPE_CHOICES = (
        (0, '系统赠送'),
        (1, '用户主动领取'),
    )
    COUPON_TYPE_CHOICES = (
        (0, '全场通用'),
        (1, '指定科室'),
    )
    name = models.CharField(max_length=20, verbose_name='优惠卷名称')
    discount_type = models.SmallIntegerField(choices=DISCOUNT_TYPE_CHOICES,default=1, verbose_name='优惠类型')
    get_type = models.SmallIntegerField(choices=GET_TYPE_CHOICES,default=0, verbose_name='获取方式')
    coupon_type = models.SmallIntegerField(choices=COUPON_TYPE_CHOICES,verbose_name='优惠卷类型')
    total = models.IntegerField(blank=True,null=True,default=100, verbose_name='优惠卷总量')
    has_total = models.IntegerField(blank=True,null=True,default=100, verbose_name='剩余数量')
    condition = models.IntegerField(blank=True,default=0, verbose_name='满足优惠卷使用的价格条件')
    start_time = models.DateTimeField(verbose_name='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间')
    sale = models.CharField(max_length=100,verbose_name='优惠公式',help_text="例如：实例:*0.8表示打八折<br>实例:-18表示在总价上减去18元")

    class Meta:
        db_table = "dx_coupon"
        verbose_name = "优惠券"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CouponDepartmentModel(models.Model):
    coupon = models.ForeignKey(CouponModel, on_delete=models.CASCADE,db_constraint=False,
                               related_name="cd_coupon", verbose_name='优惠卷')
    department = models.ForeignKey(Department, on_delete=models.CASCADE,db_constraint=False,
                                   related_name="cd_department", verbose_name='科室')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = "dx_coupon_department"
        verbose_name = "优惠券与科室关系表"
        verbose_name_plural = verbose_name



class CouponLogModel(models.Model):
    USE_CHOICE = (
        (0, '未使用'),
        (1, '已使用'),
        (2, '已过期'),
    )

    user = models.ForeignKey(UserModel, on_delete=models.CASCADE,db_constraint=False,
                             related_name="log_user", verbose_name='用户')
    coupon = models.ForeignKey(CouponModel, on_delete=models.CASCADE,db_constraint=False,
                               related_name="log_coupon", verbose_name='优惠卷')
    use_time = models.DateTimeField(null=True,blank=True, verbose_name='使用时间')
    use_status = models.SmallIntegerField(choices=USE_CHOICE, default=0, verbose_name='使用状态')

    class Meta:
        db_table = "dx_coupon_log"
        verbose_name = "优惠券发放与日志管理类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.coupon.name}发放与使用日志"









